import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hc_bot_pro/chat/state.dart';
import 'package:hc_bot_pro/common/ui/qr.dart';
import 'package:lottie/lottie.dart';
import 'package:siri_wave/siri_wave.dart';

import '../../controller.dart';
import 'doctor.dart';

class SiriView extends StatelessWidget {
  SiriView({super.key});

  @override
  Widget build(BuildContext context) {
    final vc = Get.find<ChatController>();
    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        DoctorView(),
        Positioned(child: QrPanel(), top: 10.h, right: 10.w),
        Positioned(
          bottom: 22.h,
          child: Container(
            height: 120.w,
            width: 260.w,
            padding: EdgeInsets.only(top: 10.h),
            decoration: BoxDecoration(
              color: Colors.white.withOpacity(0.7),
              borderRadius: BorderRadius.circular(14.sp),
            ),
            child: createView(vc.state),
          ),
        ),
      ],
    );
  }

  Widget createView(ChatState state) {
    switch (state.status) {
      case HCAIBotStatus.speaking:
        return _SayingView();
      case HCAIBotStatus.sleeping:
        return _SpleepingView();
      default:
        return _ListeningView();
    }
  }
}

class _ListeningView extends StatelessWidget {
  const _ListeningView({super.key});

  @override
  Widget build(BuildContext context) {
    final vc = Get.find<ChatController>();

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          vc.state.stateTip,
          style: TextStyle(fontSize: 15.sp, color: Colors.black),
        ),
        Lottie.asset(
          "assets/images/loading_lottie.json",
          width: 55.w,
          height: 55.w,
          fit: BoxFit.cover,
        ),
      ],
    );
  }
}

class _SayingView extends StatelessWidget {
  _SayingView({super.key});

  final controller = IOS9SiriWaveformController(
    amplitude: 0.8,
    color1: Colors.red,
    color2: Colors.green,
    color3: Colors.blue,
    speed: 0.15,
  );

  @override
  Widget build(BuildContext context) {
    return SiriWaveform.ios9(
      controller: controller,
      options: IOS9SiriWaveformOptions(height: 80, width: 250.w),
    );
  }
}

class _SpleepingView extends StatelessWidget {
  const _SpleepingView({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedTextKit(
        animatedTexts: [
          WavyAnimatedText(
            '请说“你好小惠”唤醒我!',
            textStyle: TextStyle(fontSize: 22.sp, fontWeight: FontWeight.bold),
            speed: const Duration(milliseconds: 200),
          ),
        ],

        totalRepeatCount: 4,
        pause: 1.seconds,
        displayFullTextOnTap: true,
        stopPauseOnTap: true,
        repeatForever: true,
      ),
    );
  }
}
